package com.qiaoba.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.qiaoba.common.base.constant.BaseConstant;
import com.qiaoba.module.system.entity.SysUserPost;
import com.qiaoba.module.system.mapper.SysUserPostMapper;
import com.qiaoba.module.system.service.SysUserPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 用户岗位关联 服务层实现
 *
 * @author ailanyin
 * @date 2023-09-10 21:35:59
 */
@Service
@RequiredArgsConstructor
public class SysUserPostServiceImpl implements SysUserPostService {

    private final SysUserPostMapper sysUserPostMapper;

    @Override
    public void updateUserPost(String userId, Set<String> postIds, boolean isUpdate) {
        if (isUpdate) {
            deleteByUserId(userId);
        }
        if (CollUtil.isEmpty(postIds)) {
            return;
        }
        List<SysUserPost> list = new ArrayList<>();
        for (String postId : postIds) {
            list.add(new SysUserPost(userId, postId));
        }
        Db.saveBatch(list);
    }

    @Override
    public void deleteByUserId(String userId) {
        deleteByUserIds(CollUtil.toList(userId));
    }

    @Override
    public void deleteByUserIds(List<String> userIds) {
        sysUserPostMapper.deleteByUserIds(userIds);
    }

    @Override
    public List<String> selectUsedPostNameByIds(List<String> postIds) {
        return sysUserPostMapper.selectUsedPostNameByIds(postIds);
    }

    @Override
    public List<String> selectPostIdsByUserId(String userId) {
        return sysUserPostMapper.selectPostIdsByUserId(userId);
    }

    @Override
    public String selectPostGroup(String userId) {
        List<String> list = sysUserPostMapper.selectPostNamesByUserId(userId);
        return CollUtil.isNotEmpty(list) ? StrUtil.join(BaseConstant.LINE_JOIN_STR, list) : StrUtil.EMPTY;
    }
}
